// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'yiibook-pro-yo19d'
})

// 云函数入口函数
exports.main = async (event, context) => {
  const _ = cloud.database().command
  const result = await cloud.database().collection('exams')
  .where({
    examDate: _.gte(event.examDate)
  })
  .skip((event.page - 1) * event.pageSize)
  .limit(event.pageSize)
  .orderBy('examDate', 'asc')
  .orderBy('startTime', 'asc')
  .get()
  let taskList = result.data;

  for(var i = 0; i < taskList.length; i++) {
    var subscribe = await cloud.database().collection('subscribes')
      .where({
        openid: event.openid,
        examId: taskList[i]._id,
      }).get()
    taskList[i].isSubscribe = subscribe.data.length > 0 ? 1 : 0
  }
  return taskList
}